package com.example.dongshihao.widgettest_211dsh;

import android.content.Intent;
import android.media.Image;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv;
    private EditText ed;
    private Button btngettext, btnsetimg;
    private ImageButton ibtn;
    private ImageView iv;
    private RadioButton rdnan,rdnv;
    private RadioGroup rg;
    private CheckBox cblan,cbzu,cbyou;
    private Button btngetender,btngetfun;

    private String strfun="";//保存兴趣的字符串


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

    }

    private void init() {
        tv = (TextView) findViewById(R.id.textView);
        ed = (EditText) findViewById(R.id.editText);
        btngettext = (Button) findViewById(R.id.button1);
        btnsetimg = (Button) findViewById(R.id.button2);
        iv = (ImageView) findViewById(R.id.imageView);
        ibtn = (ImageButton) findViewById(R.id.imageButton);

        rdnan=(RadioButton) findViewById(R.id.radioButton2);
        rdnv=(RadioButton) findViewById(R.id.radioButton);
        rg=(RadioGroup) findViewById(R.id.rg1);
        btngetender = (Button) findViewById(R.id.button);
        btngetfun = (Button) findViewById(R.id.button3);
        cblan=(CheckBox) findViewById(R.id.checkBox3);
        cbzu=(CheckBox) findViewById(R.id.checkBox2);
        cbyou=(CheckBox) findViewById(R.id.checkBox);

        btngettext.setOnClickListener(this);
        btnsetimg.setOnClickListener(this);
        ibtn.setOnClickListener(this);

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId)
                {
                    case R.id.radioButton: //female
                        tv.setText("女");
                        break;
                    case  R.id.radioButton2:
                        tv.setText("男");
                        break;

                }
            }
        });

        cblan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) //checkBox被勾选
                    strfun+="篮球+";
                else //checkBox未被勾选
                    strfun=strfun.replace("篮球+","");
                tv.setText(strfun);
            }
        });

        cbzu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) //checkBox被勾选
                    strfun+="足球+";
                else //checkBox未被勾选
                    strfun=strfun.replace("足球+","");
                tv.setText(strfun);
            }
        });

        cbyou.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) //checkBox被勾选
                    strfun+="游泳+";
                else //checkBox未被勾选
                    strfun=strfun.replace("游泳+","");
                tv.setText(strfun);
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1://设置文本
                String str = ed.getText().toString();
                tv.setText(str);
                break;
            case R.id.button2://设置图片
                iv.setImageResource(R.drawable.a60);
                break;
            case R.id.imageButton: //图片按钮
                Intent intent = new Intent(MainActivity.this, NewActivity_211dsh.class);
                intent.putExtra("param1",ed.getText().toString());
                intent.putExtra("param2",3);
                startActivity(intent);
                break;
            case R.id.button://获取性别
                if(rdnan.isChecked())
                    tv.setText("男");
                if(rdnv.isChecked())
                    tv.setText("女");
                break;
            case R.id.button3://获取兴趣
                String msg="";
                if(cblan.isChecked()) msg+="篮球 ";
                if(cbzu.isChecked())  msg+="足球 ";
                if(cbyou.isChecked()) msg+="游泳 ";

                tv.setText(msg);
                break;
        }
    }
}
